package com.CGFrame.struts.bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.CGFrame.struts.POJOs.Employee;
import com.CGFrame.struts.POJOs.Task;
import com.CGFrame.struts.bean.task.TaskBean;

public class ReportDemoBean {
	private String name=null;
	private List<TaskPeriod> taskPeriods=new ArrayList<TaskPeriod>();
	
	public static List getFinishedReportDemoBean(String begin,String end,Integer projectId) throws Exception{
		//从task表中取得在开始和结束时间之间的任务
		List<Task> tasks=getFinishedTasksByPeriod(begin,end,projectId);
		
		List<ReportDemoBean> beans=new ArrayList<ReportDemoBean>();
		
		//从员工表中取得所有员工名字
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List employees=pop.Query("Employee","status.statusId,10",null,null);
		for(Iterator iter=employees.iterator();iter.hasNext();){
			Employee emp=(Employee)iter.next();
			ReportDemoBean rdb=new ReportDemoBean();
			rdb.setName(emp.getEmpName());
			beans.add(rdb);
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date est;
		Date eet;
		Date beginDate=sdf.parse(begin);
		Date endDate=sdf.parse(end);
		int length=compareDay(beginDate,endDate);
		int indexOfBegin;
		int indexOfEnd;
		for(Iterator iter=tasks.iterator();iter.hasNext();){
			Task task=(Task)iter.next();
			String name=task.getReguser().getEmployee().getEmpName();
			for(Iterator iter2=beans.iterator();iter2.hasNext();){
				ReportDemoBean bean=(ReportDemoBean)iter2.next();
				if(bean.getName().equals(name)){
					est=task.getExactStartTime();
					eet=task.getExactEndTime();
					if(est.before(beginDate) & !eet.before(endDate)){
						indexOfBegin=0;
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(!est.before(beginDate) & est.before(endDate) & !eet.before(endDate)){
						indexOfBegin=compareDay(est,beginDate);
						indexOfEnd=length;
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(est.before(beginDate) & !eet.before(beginDate) & eet.before(endDate)){
						indexOfBegin=0;
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(!est.before(beginDate) & est.before(endDate) & !eet.before(beginDate) & eet.before(endDate)){
						indexOfBegin=compareDay(est,beginDate);
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}
				}
			}	
		}
		return beans;
	}
	
	public static List getWorkingReportDemoBean(String begin,String end,Integer projectId) throws Exception{
		
		List<Task> tasks=getWorkingTasksByPeriod(begin,end,projectId);
		
		List<ReportDemoBean> beans=new ArrayList<ReportDemoBean>();
		
		//从员工表中取得所有员工名字
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List employees=pop.Query("Employee","status.statusId,10",null,null);
		for(Iterator iter=employees.iterator();iter.hasNext();){
			Employee emp=(Employee)iter.next();
			ReportDemoBean rdb=new ReportDemoBean();
			rdb.setName(emp.getEmpName());
			beans.add(rdb);
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date est;
		Date eet;
		Date beginDate=sdf.parse(begin);
		Date endDate=sdf.parse(end);
		int length=compareDay(beginDate,endDate);
		int indexOfBegin;
		int indexOfEnd;
		Calendar cal=Calendar.getInstance();
		for(Iterator iter=tasks.iterator();iter.hasNext();){
			Task task=(Task)iter.next();
			TaskBean tb=new TaskBean(task);
			String name=task.getReguser().getEmployee().getEmpName();
			for(Iterator iter2=beans.iterator();iter2.hasNext();){
				ReportDemoBean bean=(ReportDemoBean)iter2.next();
				if(bean.getName().equals(name) && tb.getVe()!=null){
					est=tb.getVe();
					Integer days=getWorkDays(task.getExceptHours());
					cal.setTime(est);
					cal.add(Calendar.DAY_OF_MONTH, days);
					eet=cal.getTime();
					if(est.before(beginDate) & !eet.before(endDate)){
						indexOfBegin=0;
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(!est.before(beginDate) & est.before(endDate) & !eet.before(endDate)){
						indexOfBegin=compareDay(est,beginDate);
						indexOfEnd=length;
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(est.before(beginDate) & !eet.before(beginDate) & eet.before(endDate)){
						indexOfBegin=0;
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}else if(!est.before(beginDate) & est.before(endDate) & !eet.before(beginDate) & eet.before(endDate)){
						indexOfBegin=compareDay(est,beginDate);
						indexOfEnd=compareDay(eet,beginDate);
						TaskPeriod tp=new TaskPeriod(task.getTaskName(),indexOfBegin,indexOfEnd,task.getStatus().getStatusName());
						bean.getTaskPeriods().add(tp);
					}
				}
			}	
		}
		return beans;
	}
	
	//查找已完成的任务，projectId为null表示查询所有项目
	private static List<Task> getFinishedTasksByPeriod(String begin,String end,Integer projectId){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List<Task> tasks=pop.Query("Task", "exactStartTime!,null,exactEndTime!,null,reguser!,null", null, null);
		List<Task> ts=new ArrayList<Task>();
		if(projectId != null){
			for(Iterator iter=tasks.iterator();iter.hasNext();){
				Task task=(Task)iter.next();
				TaskBean tb=new TaskBean(task);
				if(tb.getProjectId().equals(projectId)){
					ts.add(task);
				}
			}
			return ts;
		}
		return tasks;
	}
	
	//查找未完成的任务，projectId为null表示查询所有项目
	private static List<Task> getWorkingTasksByPeriod(String begin,String end,Integer projectId){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List<Task> tasks=pop.Query("Task", "exceptHours!,null,reguser!,null", null, null);
		if(projectId != null){
			for(Iterator iter=tasks.iterator();iter.hasNext();){
				Task task=(Task)iter.next();
				TaskBean tb=new TaskBean(task);
				if(!tb.getProjectId().equals(projectId)){
					tasks.remove(task);
				}
			}
		}
		return tasks;
	}
	
	
	/*比较天数的差距
	 * 负数表示end在begin之前，0表示同天，正数表示end在begin之后
	 * @param begin,end 比较日期
	 * @return 天数的差
	*/
	public static int compareDay(Date begin,Date end){
		//如果end 早于 begin ,对换值
		if(begin.after(end)){
			Date temp = end;
			end=begin;
			begin=temp;
		}
		//一天86400000豪秒
		long beginTime=begin.getTime();
		long endTime=end.getTime();
		long time=endTime-beginTime;
		int days=(int)(time/86400000);
		//去除相差的整数天后,判断是否在同一天,不是同一天则加1.
		Date newDate=new Date(beginTime+(time%86400000));
		if(newDate.getDate()!=begin.getDate()){
			days+=1;
		}
		return days;
	}
	private static Integer getWorkDays(Integer hours){
		int days=hours/8;
		if((hours%8)!=0){
			days+=1;
		}
		return days;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<TaskPeriod> getTaskPeriods() {
		return taskPeriods;
	}

	public void setTaskPeriods(List<TaskPeriod> taskPeriods) {
		this.taskPeriods = taskPeriods;
	}
	
	public static class TaskPeriod{
		private String taskName;
		private int begin;
		private int end;
		private String statusName;
		
		public String getColor() {
			return statusName;
		}

		public void setColor(String color) {
			this.statusName = color;
		}

		public TaskPeriod(String taskName,int begin,int end,String color){
			this.taskName=taskName;
			this.begin=begin;
			this.end=end;
			this.statusName=color;
		}
		
		public int getBegin() {
			return begin;
		}
		public void setBegin(int begin) {
			this.begin = begin;
		}
		public int getEnd() {
			return end;
		}
		public void setEnd(int end) {
			this.end = end;
		}
		public String getTaskName() {
			return taskName;
		}
		public void setTaskName(String taskName) {
			this.taskName = taskName;
		}	
	}	
}
